package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.region.Region;
import com.heima.pojo.region.RegionPageResult;
import com.heima.pojo.region.RegionQueryParam;
import com.heima.pojo.region.RegionVO;
import com.heima.service.RegionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@RequiredArgsConstructor
@Service
@Slf4j
public class RegionServiceImpl implements RegionService {
    private final RegionMapper regionMapper;

    /**
     * 区域搜素
     * @param param
     * @return
     */
    @Override
    public RegionPageResult search(RegionQueryParam param) {
        log.info("区域搜索参数:{}",param);
        PageHelper.startPage(param.getPageIndex(), param.getPageSize());
        Page<RegionVO> page =regionMapper.search(param);
        String pageIndex = String.valueOf(param.getPageIndex());
        String pageSize = String.valueOf(param.getPageSize());
        String pages = String.valueOf(page.getPages());
        String total = String.valueOf(page.getTotal());
        return new RegionPageResult(pageIndex,pageSize,pages,total, page.getResult());
    }

    /**
     * 新增区域
     * @param region
     */
    @Override
    public void addRegion(Region region) {
        log.info("新增区域:{}",region);
        long timeId = System.currentTimeMillis();
        region.setRegionId(timeId+"");
        regionMapper.addRegion(region);
    }

    /**
     * 修改区域
     * @param region
     */
    @Override
    public void updateRegion(Region region) {
        regionMapper.updateRegion(region);
    }

    /**
     * 根据id查询区域
     * @param regionId
     * @return
     */
    @Override
    public RegionVO getById(String regionId) {
        return regionMapper.getById(regionId);
    }

    /**
     * 删除区域
     * @param regionId
     */
    @Override
    public void deleteRegion(String regionId) {
        regionMapper.deleteRegion(regionId);
    }
}
